package com.shaoxi.risk.app;

import com.shaoxi.risk.api.RiskControlFacade;
import com.shaoxi.risk.api.dto.RiskReuslt;
import com.shaoxi.risk.api.req.PriceRiskReq;
import com.shaoxi.risk.domain.context.PriceRiskContext;
import com.shaoxi.risk.domain.model.RiskEvent;
import com.shaoxi.risk.domain.model.RiskInfo;
import com.shaoxi.risk.domain.repository.RiskEventRepo;
import com.shaoxi.risk.domain.service.RiskControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 风控服务
 * 应用层，调用领域服务，完成业务编排
 */
@Service
public class RiskControlFacadeImpl implements RiskControlFacade {

    @Autowired
    private RiskControlService riskControlService;

    @Autowired
    private RiskEventRepo riskEventRepo;


    @Override
    public RiskReuslt priceRiskCheck(String eventCode, PriceRiskReq riskReq) {
        //风控上下文构建
        RiskEvent riskEvent = riskEventRepo.getRiskEvent(eventCode);
        PriceRiskContext priceRiskContext = buildPriceRiskContext(riskEvent, riskReq);

        //风险识别
        RiskInfo riskInfo = riskControlService.judeRisk(priceRiskContext);

        //风险处置
        riskControlService.disposalRisk(riskEvent, riskInfo);

        // TODO 构建结果返回

        return null;
    }

    private PriceRiskContext buildPriceRiskContext(RiskEvent riskEvent, PriceRiskReq riskReq) {
        PriceRiskContext priceRiskContext = new PriceRiskContext();
        priceRiskContext.setRiskEvent(riskEvent);
        priceRiskContext.setPriceRiskReq(riskReq);
        return priceRiskContext;
    }
}
